﻿using AutoMapper;
using FileHelpers;
using System.Collections.Generic;
using System.Linq;

namespace ZillowApiClient.Data.Model
{
    [DelimitedRecord(",")]
    public class InputRecord
    {
        public string address;
        public string city;
        public string state;

        [FieldHidden]
        public List<result> results;

        [FieldHidden]
        public string Exceptions;

        public OutputRecord ToOutput()
        {
            var o = Mapper.Map<OutputRecord>(this);
            o.Exceptions = Exceptions?.Replace("\r\n", " ").Replace(",", "|") ?? string.Empty;
            if (results == null)
                return o;
            o.NumberOfHits = results?.Count ?? 0;
            o.LastUpdated = results[0]?.zestimate?.last_updated;
            o.Zestimate = (results[0]?.zestimate?.amount).GetValueOrDefault();
            if (o.NumberOfHits > 1)
                o.OtherHits =
                    results.Skip(1)
                        .Select(x => x.address.street + " " + x.address.city + " - " + x.zestimate.amount)
                        .Aggregate((x, y) => x + " | " + y);

            return o;
        }
    }
}